package com.kun.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

/**
 * @description: fastjson
 * @author: KM
 * 2018年3月23日 下午3:34:14
 */
@Configuration
public class ConverterConfig {
	
		//需要引入fastjson依赖库
		@Bean
		public HttpMessageConverters fastJsonHttpMessageConverters(){
			//1、需要先定义一个convert转换消息的对象
			FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
			
			//2、添加fastJson的配置信息，比如：是否格式化返回的json数据
			FastJsonConfig fastJsonConfig = new FastJsonConfig();
			fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
			fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteNullStringAsEmpty);
			fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
			//A、处理中文乱码问题
	        List<MediaType> fastMediaTypes = new ArrayList<>();
	        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
	        fastConverter.setSupportedMediaTypes(fastMediaTypes);
			
			//3、在convert中添加配置信息
			fastConverter.setFastJsonConfig(fastJsonConfig);
			
			//4、返回
			HttpMessageConverter<?> converter = fastConverter;
			return new HttpMessageConverters(converter);
		}
		

}
